package com.ruoyi.member.service.impl;

import com.ruoyi.member.domain.MemberWallet;
import com.ruoyi.member.mapper.MemberWalletMapper;
import com.ruoyi.member.service.IMemberWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;

/**
 * 会员钱包Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-22
 */
@Service
public class MemberWalletServiceImpl implements IMemberWalletService {

    @Autowired
    private MemberWalletMapper memberWalletMapper;

    /**
     * 查询会员钱包
     *
     * @param walletId 会员钱包主键
     * @return 会员钱包
     */
    @Override
    public MemberWallet selectMemberWalletById(Long walletId) {
        return memberWalletMapper.selectMemberWalletById(walletId);
    }

    /**
     * 查询会员钱包列表
     *
     * @param memberWallet 会员钱包
     * @return 会员钱包集合
     */
    @Override
    public List<MemberWallet> selectMemberWalletList(MemberWallet memberWallet) {
        return memberWalletMapper.selectMemberWalletList(memberWallet);
    }

    /**
     * 根据会员ID查询钱包
     *
     * @param memberId 会员ID
     * @return 会员钱包
     */
    @Override
    public MemberWallet selectMemberWalletByMemberId(Long memberId) {
        return memberWalletMapper.selectMemberWalletByMemberId(memberId);
    }

    /**
     * 新增会员钱包
     *
     * @param memberWallet 会员钱包
     * @return 结果
     */
    @Override
    public int insertMemberWallet(MemberWallet memberWallet) {
        return memberWalletMapper.insertMemberWallet(memberWallet);
    }

    /**
     * 修改会员钱包
     *
     * @param memberWallet 会员钱包
     * @return 结果
     */
    @Override
    public int updateMemberWallet(MemberWallet memberWallet) {
        return memberWalletMapper.updateMemberWallet(memberWallet);
    }

    /**
     * 批量删除会员钱包
     *
     * @param walletIds 需要删除的会员钱包主键集合
     * @return 结果
     */
    @Override
    public int deleteMemberWalletByIds(Long[] walletIds) {
        return memberWalletMapper.deleteMemberWalletByIds(walletIds);
    }

    /**
     * 删除会员钱包信息
     *
     * @param walletId 会员钱包主键
     * @return 结果
     */
    @Override
    public int deleteMemberWalletById(Long walletId) {
        return memberWalletMapper.deleteMemberWalletById(walletId);
    }

    /**
     * 充值
     *
     * @param memberId 会员ID
     * @param amount   充值金额
     * @return 结果
     */
    @Override
    public int recharge(Long memberId, BigDecimal amount) {
        return memberWalletMapper.recharge(memberId, amount);
    }

    /**
     * 扣款
     *
     * @param memberId 会员ID
     * @param amount   扣款金额
     * @return 结果
     */
    @Override
    public int deduct(Long memberId, BigDecimal amount) {
        MemberWallet wallet = selectMemberWalletByMemberId(memberId);
        // TODO: 需要修改为使用Integer类型的gold字段
        if (wallet == null) {
            return 0; // 钱包不存在
        }
        return memberWalletMapper.deduct(memberId, amount);
    }

    /**
     * 增加积分
     *
     * @param memberId 会员ID
     * @param points 积分数量
     * @return 结果
     */
    @Override
    public int addPoints(Long memberId, Integer points) {
        // 积分在 member_user 表中，需要调用 MemberUserMapper
        return memberWalletMapper.addPoints(memberId, points);
    }

    /**
     * 使用积分
     *
     * @param memberId 会员ID
     * @param points 积分数量
     * @return 结果
     */
    @Override
    public int usePoints(Long memberId, Integer points) {
        // 检查积分是否足够需要查询 member_user 表
        // 这里简化处理，直接调用 mapper
        return memberWalletMapper.usePoints(memberId, points);
    }

    /**
     * 更新VIP等级
     *
     * @param memberId 会员ID
     * @param vipLevel VIP等级
     * @return 结果
     */
    @Override
    public int updateVipLevel(Long memberId, Integer vipLevel) {
        // VIP等级在 member_user 表中，需要调用 MemberUserMapper
        return memberWalletMapper.updateVipLevel(memberId, vipLevel);
    }
}